//
//  SC_Search_RoomCell.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/4/25.
//

import UIKit
import SnapKit
import Kingfisher

class SC_Search_RoomCell: APBaseTableViewCell {
    private var bgView: UIView!
    private var avatarImageView: UIImageView!
    private var roomNameLabel: UILabel!
    private var roomIDLabel: UILabel!
    private var nicknameLabel: UILabel!
    
    override func initUI() {
        super.initUI()
        
        backgroundColor = .clear
        bgView = UIView().then({ make in
            contentView.addSubview(make)
            make.backgroundColor = .white
            make.layer.cornerRadius = 10
            make.layer.masksToBounds = true
        })
        
        avatarImageView = UIImageView().then({ make in
            bgView.addSubview(make)
            make.contentMode = .scaleAspectFill
        })
        
        roomNameLabel = UILabel().then({ make in
            bgView.addSubview(make)
            make.font(.ap.medium(14)).textColor(.ap.hex("191702"))
        })
        
        roomIDLabel = UILabel().then({ (make) in
            bgView.addSubview(make)
            make.font(.ap.regular(12)).textColor(.ap.hex("7D7D85"))
        })
        
        nicknameLabel = UILabel().then({ (make) in
            bgView.addSubview(make)
            make.font(.ap.regular(12)).textColor(.ap.hex("7D7D85"))
        })
    }
    
    override func makeConstraints() {
        super.makeConstraints()
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.bottom.equalTo(-10)
            make.top.equalToSuperview()
        }
        
        avatarImageView.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(108)
        }
    
        roomNameLabel.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(avatarImageView.snp.right).offset(8)
            make.right.equalTo(-12)
        }
        
        roomIDLabel.snp.makeConstraints { make in
            make.top.equalTo(avatarImageView).offset(37)
            make.left.equalTo(avatarImageView.snp.right).offset(8)
            make.height.equalTo(18)
        }
        
        nicknameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(roomIDLabel)
            make.left.equalTo(roomIDLabel.snp.right).offset(6)
        }
    }
    
    override func setup(_ model: Any?) {
        guard let model = model as? SC_Search_RoomModel else { return }
        if let cover = model.cover, !cover.isEmpty {
            avatarImageView.kf.setImage(with: URL(string: cover))
        }
        
        roomNameLabel.text = model.title

        roomIDLabel.text = "ID: \(model.id ?? 0)"
        
        if model.type != .MANY_PEOPLE_ROOM {
            nicknameLabel.text = "昵称:\(model.owner_nickname ?? "")"
            nicknameLabel.isHidden = false
        } else {
            nicknameLabel.isHidden = true
        }
    }
}

extension SC_Search_RoomCell: APView_Height_Protocol {
    static func viewHeight(with model: Any?) -> CGFloat {
        return 108
    }
}
